package com.leoandroid.tool.toolsbox.largeread.activity

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.leoandroid.tool.toolsbox.largeread.R
import com.leoandroid.tool.toolsbox.largeread.databinding.ActivityXingzuoBinding
import com.leoandroid.tool.toolsbox.largeread.fragment.XingFragment

class XingZuoActivity : AppCompatActivity(){

    var bindingF : ActivityXingzuoBinding?=null
    val binding get() = bindingF!!
    var mFragments:ArrayList<Fragment> = ArrayList()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bindingF =  ActivityXingzuoBinding.inflate(layoutInflater)
        setContentView(binding.root)
        showFragment(XingFragment.getInstance())
    }

    private fun showFragment(fragment: Fragment){
        hiddenFragments()
        val fragmentTransaction = supportFragmentManager.beginTransaction();
        if(fragment!=null && mFragments.contains(fragment)){
            fragmentTransaction.show(fragment)
        } else {
            if(fragment!= null && !fragment.isAdded){
                fragmentTransaction.add(R.id.containerFr,fragment)
            }
        }
        fragmentTransaction.commitNowAllowingStateLoss()
    }

    private fun hiddenFragments(){
        if(mFragments.size>0){
            for(fragment in mFragments){
                val fragmentTransient = supportFragmentManager.beginTransaction()
                fragmentTransient.hide(fragment)
                fragmentTransient.commitNowAllowingStateLoss()
            }
        }
    }
}